// 2013-7-29下午02:28:51  net.betou.trade.main.dao.impl.CategoryDaoImpl.java

package net.betou.trade.main.dao.impl;

import java.util.List;

import net.betou.common.hibernate3.Finder;
import net.betou.common.hibernate3.HibernateBaseDao;
import net.betou.common.page.Pagination;
import net.betou.trade.entity.main.Category;
import net.betou.trade.main.dao.CategoryDao;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

/**
 * 
 * @author 叶装装
 */
@Repository
public class CategoryDaoImpl extends HibernateBaseDao< Category, Integer > implements CategoryDao{

	@Override
	protected Class< Category > getEntityClass( ) {
		return Category.class;
	}

	public Category deleteById( Integer id ) {
		Category bean = get( id );
		if( bean != null ){
			getSession( ).delete( bean );
		}
		
		return bean;
	}

	public Category findById( Integer id ) {
		return get( id );
	}

	public Category findMyCategory( Integer id, Integer selfId ) {
		String hql = "from Category bean where bean.id=? and bean.admin.id=?";
		return ( Category ) this.findUnique( hql, id, selfId );
	}

	public Pagination getPage( Integer selfId, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageNo, int pageSize ) {
		Finder f = Finder.create( " from Category bean where 1=1 " );
		if( selfId != null ){
			f.append( " and bean.admin.id=:id" );
			f.setParam( "id", selfId );
		}
		createSearch( f, searchProperty, searchValue );
		createOrder( f, orderProperty, orderDirection, "id" );
		
		return find( f, pageNo, pageSize );
	}
	
	public Pagination getPage( Integer selfId, String term, String orderProperty, String orderDirection, int pageNo, int pageSize ){
		Finder f = Finder.create( " from Category bean where ( bean.name like :name or bean.pinyin like :pinyin ) " );
		if( selfId != null ){
			f.append( " and bean.admin.id=:id" );
			f.setParam( "id", selfId );
		}
		term = "%" + term + "%";
		f.setParam( "name", term );
		f.setParam( "pinyin", term );
		
		
		createOrder( f, orderProperty, orderDirection, "id" );
		
		return find( f, pageNo, pageSize );
	}
	
	public Pagination getPageByCriteria( String sql, String orderProperty, String orderDirection, int pageNo, int pageSize ){
		Criteria c = this.createCriteria( sql ); // 创建对象
		Pagination p = this.findByCriteria( c, pageNo, pageSize );
		
		this.createOrder( c, orderProperty, orderDirection, "id" );
		
		return p;
	}
	
	
	public Category save( Category bean ) {
		getSession( ).save( bean );
		
		return bean;
	}
	
	public int countByName( String name ){
		String hql = "select count(*) from Category bean where bean.name=:name";
		Query query = getSession( ).createQuery( hql );
		query.setParameter( "name", name );
		return ( ( Number ) query.iterate( ).next( ) ).intValue( );
	}
	
	@SuppressWarnings( "unchecked" )
	public List< Category > findListByName( String name ){
		String hql = "from Category bean where ( bean.name like ? or bean.pinyin like ? ) and bean.admin.id=52";
		Query query = createQuery( hql, name, name );
		
		
		return query.list( );
	}
	
	public Category findByName( String name ){
		return ( Category )findUniqueByProperty( "name", name );
	}
}
